//*** Guidance generated code ***//

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using ProjectBase.Core;
using ProjectBase.Core.Tools.Import;
using ProjectBase.Mvc.Controllers;

using $prjsn$.Common;
using $prjsn$.Common.Business.Services;
using $prjsn$.Entity.Services;

namespace $safeprojectname$.Tools.Import.Excel
{
    /// <summary>
    /// Basic tests for Language
    /// </summary>
    [TestClass]
    public class ImportLangTest : EntityTestBase<ILangFacade>
    {
        [TestMethod]
        public void ImportLanguages()
        {
            IImporterData model;
            IExcelImporter importer = GetImporter(out model);

            var result = importer.Execute(model);

            Assert.IsFalse(Messages.ContainsError(), Messages.GetErrorMessage());
            Assert.IsTrue(result.Is(), "import was not successful");

            Assert.IsTrue(result.IsNotEmpty(), "Empty result, nothing was imported ");

            var resultValue = result.First();
            Assert.IsTrue(resultValue.ContainsPrefix("LanguageName"));
            Assert.IsTrue(resultValue.GetValue("LanguageName").AttemptedValue.Equals("cs"));

            var coll = resultValue.GetValue("NativeName").RawValue as IEnumerable<string>;
            Assert.IsTrue(coll.IsNotEmpty() && coll.Count() > 1);
        }
        [TestMethod]
        public void ImportAndBindLanguages()
        {
            IImporterData model;
            IExcelImporter importer = GetImporter(out model);

            var result = importer.Execute(model);

            Assert.IsFalse(Messages.ContainsError(), Messages.GetErrorMessage());
            Assert.IsTrue(result.Is(), "import was not successful");

            Assert.IsTrue(result.IsNotEmpty(), "Empty result, nothing was imported ");

            var resultContainer = result.First();
            Assert.IsTrue(resultContainer.Is());

            // Binding

            var lang = new Lang();
            var smart = new SmartBinder();

            var controllerContext = new ControllerContext();
            var bindingContext = new ModelBindingContext
            {
                ValueProvider = resultContainer.ToProvider(),
                ModelMetadata = ModelMetadataProviders.Current
                    .GetMetadataForType(() => lang, typeof(Lang)),
            };

            Assert.IsFalse(lang.LanguageName == "cs");
            Assert.IsFalse(lang.ShowFlag);
            Assert.IsFalse(lang.ID == 1);

            smart.BindModel(controllerContext, bindingContext);

            Assert.IsTrue(lang.LanguageName == "cs");
            Assert.IsTrue(lang.ShowFlag);
            Assert.IsFalse(lang.ID == 1); // protected?!?
        }

        [TestMethod]
        public void ImportExistinAndBindLanguages()
        {
            IImporterData model;
            IExcelImporter importer = GetImporter(out model);

            var result = importer.Execute(model);

            Assert.IsFalse(Messages.ContainsError(), Messages.GetErrorMessage());
            Assert.IsTrue(result.Is(), "import was not successful");

            Assert.IsTrue(result.IsNotEmpty(), "Empty result, nothing was imported ");

            var resultContainer = result.First();
            Assert.IsTrue(resultContainer.Is());

            // Binding

            var lang = new Lang();
            var smart = new SmartBinder();

            var controllerContext = new ControllerContext();
            var bindingContext = new ModelBindingContext
            {
                ValueProvider = resultContainer.ToProvider(),
                ModelMetadata = ModelMetadataProviders.Current
                    .GetMetadataForType(() => lang, typeof(Lang)),
            };

            Assert.IsFalse(lang.LanguageName == "cs");
            Assert.IsFalse(lang.ShowFlag);
            Assert.IsFalse(lang.ID == 1);

            smart.BindModel(controllerContext, bindingContext);

            Assert.IsTrue(lang.LanguageName == "cs");
            Assert.IsTrue(lang.ShowFlag);
            Assert.IsFalse(lang.ID == 1); // protected?!?
        }

        //[TestMethod]
        public void ImportAndBindSpecialLanguages()
        {
            Assert.Inconclusive("TODO .. problems with ");
            IImporterData model;
            IExcelImporter importer = GetImporter(out model);

            var result = importer.Execute(model);

            Assert.IsFalse(Messages.ContainsError(), Messages.GetErrorMessage());
            Assert.IsTrue(result.Is(), "import was not successful");

            Assert.IsTrue(result.IsNotEmpty(), "Empty result, nothing was imported ");

            var resultContainer = result.First();
            Assert.IsTrue(resultContainer.Is());

            // Binding

            var lang = new SpecialLang();
            lang.Address = new Address();
            var smart = new SmartBinder();

            var controllerContext = new ControllerContext();
            var bindingContext = new ModelBindingContext
            {
                ValueProvider = resultContainer.ToProvider(),
                ModelMetadata = ModelMetadataProviders.Current
                    .GetMetadataForType(() => lang, typeof(SpecialLang)),
            };

            Assert.IsFalse(lang.LanguageName == "cs");
            Assert.IsFalse(lang.ShowFlag);
            Assert.IsTrue(lang.Created == new DateTime());
            Assert.IsTrue(lang.DecimalValue == 0);
            Assert.IsTrue(lang.FloatValue == 0);
            Assert.IsTrue(lang.IntValue == 0);
            Assert.IsTrue(lang.ByteValue == 0);
            Assert.IsTrue(lang.ShortValue == 0);
            Assert.IsTrue(lang.UShortValue == 0);
            Assert.IsTrue(lang.LongValue == 0);
            Assert.IsTrue(lang.User.IsNull());
            Assert.IsTrue(lang.Updated.IsNull());
            Assert.IsTrue(lang.Names.IsEmpty());
            Assert.IsTrue(lang.ANumbers.IsEmpty());
            Assert.IsTrue(lang.Address.Street.IsEmpty());
            Assert.IsFalse(lang.ID == 1);


            smart.BindModel(controllerContext, bindingContext);

            bindingContext = new ModelBindingContext
            {
                ValueProvider = resultContainer.ToProvider(),
                ModelMetadata = ModelMetadataProviders.Current
                    .GetMetadataForType(() => lang.Address, typeof(Address)),
                ModelName = "Address"
            };
            smart.BindModel(controllerContext, bindingContext);

            Assert.IsTrue(lang.LanguageName == "cs");
            Assert.IsTrue(lang.ShowFlag);
            Assert.IsTrue(lang.DecimalValue > 0);
            Assert.IsTrue(lang.FloatValue > 0);
            Assert.IsTrue(lang.IntValue > 0);
            Assert.IsTrue(lang.ByteValue > 0);
            Assert.IsTrue(lang.ShortValue > 0);
            Assert.IsTrue(lang.UShortValue > 0);
            Assert.IsTrue(lang.LongValue > 0);
            Assert.IsTrue(lang.User.Is());
            Assert.IsTrue(lang.Created.Year > 1900);
            Assert.IsTrue(lang.Updated.Is());
            Assert.IsTrue(lang.Updated.Value.Year > 1900);
            Assert.IsFalse(lang.Names.IsEmpty());
            Assert.IsFalse(lang.ANumbers.IsEmpty());
            Assert.IsTrue(lang.Address.Street.IsNotEmpty());
            Assert.IsFalse(lang.ID == 1); // protected?!?
        }

        protected IExcelImporter GetImporter(out IImporterData model)
        {
            var importer = ProjectBase.Core.Service.Factory.CreateInstance<IExcelImporter>();
            model = ProjectBase.Core.Service.Factory.CreateInstance<IImporterData>();

            if (importer.IsNull() || model.IsNull())
            {
                Assert.Inconclusive("there is no Excel Importer (IExcelImporter or IImporterData implementation");
            }

            model.DataSource = LoadTestFile();
            if (model.DataSource.IsEmpty())
            {
                Assert.Inconclusive("No Excel file as data source was loaded");
            }
            return importer;
        }

        protected byte[] LoadTestFile()
        {
            byte[] data;
            using (var fs = new FileStream($prjsn$.Common.Project.CreateAppDataPath("Language.xlsx"),
                        FileMode.Open, FileAccess.Read))
            {
                // Create a byte array of file stream length
                data = new byte[fs.Length];

                //Read block of bytes from stream into the byte array
                fs.Read(data, 0, (int)fs.Length);

                //Close the File Stream
                fs.Close();
            }
            return data;
        }

        #region Additional test attributes
        // constructor
        [ClassInitialize]
        public static void MyClassInitialize(TestContext testContext) { }

        [ClassCleanup]
        public static void MyClassCleanup() { }

        [TestInitialize]
        public override void MyTestInitialize() { base.MyTestInitialize(); }

        [TestCleanup]
        public override void MyTestCleanup() { base.MyTestCleanup(); }
        #endregion Additional test attributes
    }
}